package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.CheckgroupDto;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.pojo.CheckGroupItem;
import com.itheima.pojo.Checkgroup;
import com.itheima.service.CheckGroupItemService;
import com.itheima.service.CheckgroupService;
import com.itheima.support.PageResult;
import com.itheima.support.QueryPageBean;
import com.itheima.vo.CheckgroupVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@DubboService
public class CheckgroupServiceImpl extends ServiceImpl<CheckgroupMapper, Checkgroup> implements CheckgroupService {

    @Resource
    private CheckGroupItemService checkGroupItemService;

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
//        IPage<Checkgroup> myPage = new Page<>();
//        myPage.setCurrent(queryPageBean.getCurrentPage());
//        myPage.setSize(queryPageBean.getPageSize());
//        this.page(myPage);
        QueryWrapper<Checkgroup> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(queryPageBean.getQueryString()) == false){
            // and (code like '%02%' or name like '%02%' or help_code like '%02%')
            queryWrapper.and((wrapper) ->{
                wrapper.like("code",queryPageBean.getQueryString()).or()
                        .like("name",queryPageBean.getQueryString()).or()
                        .like("help_code",queryPageBean.getQueryString());
            });
        }
        IPage<Checkgroup> result = this.page(new Page<Checkgroup>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);
        return new PageResult(result.getTotal(),result.getRecords());
    }

    @Override
    @Transactional
    public boolean saveGroup(CheckgroupDto checkgroupDto) {
        //1.维护主表信息(检查组)
        this.saveOrUpdate(checkgroupDto.getCheckgroup());
        //2.删除已有的关系表数据
        checkGroupItemService.deleteByGroupId(checkgroupDto.getCheckgroup().getId());
        //3.维护关系表
        /* 逐条发送插入语句
        if(checkgroupDto.getItemIds() != null){
            for(Long itemId : checkgroupDto.getItemIds()){
                CheckGroupItem checkGroupItem = new CheckGroupItem();
                checkGroupItem.setItemId(itemId);
                //获取主表主键ID
                checkGroupItem.setGroupId(checkgroupDto.getCheckgroup().getId());
                checkGroupItemService.save(checkGroupItem);
            }
        }
         */
        //批量插入数据
        if(checkgroupDto.getItemIds() != null){
            List<CheckGroupItem> list = new ArrayList<>();
            for(Long itemId : checkgroupDto.getItemIds()){
                CheckGroupItem checkGroupItem = new CheckGroupItem();
                checkGroupItem.setItemId(itemId);
                //获取主表主键ID
                checkGroupItem.setGroupId(checkgroupDto.getCheckgroup().getId());
                list.add(checkGroupItem);
            }
            checkGroupItemService.saveBatch(list);
        }
        return true;
    }

    @Override
    public List<CheckgroupVO> findBySetmealId(Long setmealId) {
        return baseMapper.findBySetmealId(setmealId);
    }

}
